/**
 * 
 */
package com.example.gametestproject;

import com.xxxx.game.*;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;

/**
 * @author HuXiaolong
 *
 */
public class ShaderTextSprite extends SpriteBasic
{
	private TextSprite[] mTextSprites = new TextSprite[3];
	private final static int max_live_time = 30;
	private int mLiveTime = max_live_time;
	public ShaderTextSprite(String txt, PointF ptStart, PointF ptEnd)
	{
		LinearPath lPath = new LinearPath(ptStart, ptEnd, mLiveTime);
		
		CompactAnimation ca = new CompactAnimation();
		ca.add(lPath);
		ca.add(this.mAnimation);
		this.setSpriteAnimation(ca);
		
		for (int i = 0; i < 3; ++ i)
		{
			// add new text sprite
			TextSprite ts = new TextSprite(txt, 20, 
					Color.argb(10, 139, 0, 255), Color.argb(10, 255, 255, 255), Color.argb(10, 0, 0, 255), 
					5, 3, TextSprite.HorizontalAlignment.AlignCenter, TextSprite.VerticalAlignment.AlignCenter);
			ts.setPosition(ptStart);
			mTextSprites[i] = ts;
		}
		
		mTextSprites[2].setTextSize(30);
		mTextSprites[2].setInnerColor1(Color.argb(255, 255, 255, 255));
		mTextSprites[2].setInnerColor2(Color.argb(255, 0, 0, 255));
		mTextSprites[2].setOuterColor(Color.argb(255, 139, 0, 255));
		
		this.setZOrder(201);
		
		this.setDrawFunc(this.mDrawing);
		this.setSpriteAnimation(ca);
	}
	public void setLiveTime(int iliveTime)
	{
		this.mLiveTime = iliveTime;
	}
	@Override
	public void makedead()
	{
		mLiveTime = 0;
	}
	
	@Override
	public boolean isLiving()
	{
		return mLiveTime > 0;
	}
	
	private IAnimation mAnimation = new AnimationBasic()
	{
		@Override
		public boolean update(ISprite sprite)
		{
			if (mLiveTime > 0)
			{
				ShaderTextSprite stx = (ShaderTextSprite)sprite;
				PointF pt = stx.getPosition();
				update_text1(stx.mTextSprites[0], pt);
				update_text2(stx.mTextSprites[1], pt);
				update_main(stx.mTextSprites[2], pt);
				-- mLiveTime;
			}
			
//			if (mLiveTime == 1) return false;
			
			if (mLiveTime <= 0)
			{
				sprite.makedead();
			}
			return mLiveTime > 0;
		}

		@Override
		public String getName()
		{
			return "ShaderTextAnimation";
		}

		@Override
		public boolean isMoving()
		{
			return true;
		}
		
		private void update_text1(TextSprite ts, PointF pt)
		{
			ts.setPosition(pt);
			if (mLiveTime >= ShaderTextSprite.max_live_time / 2)
			{
				ts.setTextSize(ts.getTextSize() + 10);
				update_sprite_alpha(ts, 7);
			}
			else if (mLiveTime > 0)
			{
				update_sprite_alpha(ts, -3);
			}
		}
		
		private void update_text2(TextSprite ts, PointF pt)
		{
			ts.setPosition(pt);
			if (mLiveTime < ShaderTextSprite.max_live_time / 2 && mLiveTime > 0)
			{
				ts.setTextSize(ts.getTextSize() + 6);
				update_sprite_alpha(ts, 10);
			}
		}
		
		private void update_main(TextSprite ts, PointF pt)
		{
			ts.setPosition(pt);
			if (mLiveTime >= ShaderTextSprite.max_live_time / 2)
			{
				ts.setTextSize(ts.getTextSize() + 1);
			}
			else
			{
				ts.setTextSize(ts.getTextSize() - 1);
			}
		}
		
		private void update_sprite_alpha(TextSprite ts, int diff_alpha)
		{
			ts.setOuterColor(updatealpha(ts.getOuterColor(), diff_alpha));
			ts.setInnerColor1(updatealpha(ts.getInnerColor1(), diff_alpha));
			ts.setInnerColor2(updatealpha(ts.getInnerColor2(), diff_alpha));
		}
		
		private int updatealpha(int clr, int diff_alpha)
		{
			int alpha = Math.max(0, Math.min(255, Color.alpha(clr) + diff_alpha));
			return  (alpha << 24) | (clr & 0x00FFFFFF);
		}
		
	};

	private ICanvasDrawing mDrawing = new ICanvasDrawing()
	{
		@Override
		public boolean paint(Canvas canvas, ISprite sprite)
		{
			ShaderTextSprite stx = (ShaderTextSprite)sprite;
			for (TextSprite ts : stx.mTextSprites)
			{
				ts.paint(canvas);
			}
			
			return true;
		}

		@Override
		public String getName()
		{
			return "ShaderTextDrawing";
		}
	};
}
